package cn.dengta.webapp.trade.entity;

import java.util.Date;

import cn.dengta.webapp.trade.model.TradeTypeCore;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 用户金币账户
 *
 * @author Ready
 * @since 2022-11-21
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("tr_user_gold")
public class UserGold {

	private static final long serialVersionUID = 1L;

	/** 用户id主键 */
	@TableId
	Long userId;
	/** 账户总额（= active_amount + frozen_amount） */
	Integer totalAmount;
	/** 可用额度 */
	Integer activeAmount;
	/** 冻结额度 */
	Integer frozenAmount;
	/** 添加时间 */
	Date addTime;
	/** 更新时间 */
	Date updateTime;

	/** 初始化默认数据 */
	public static UserGold initDefault(Long userId) {
		UserGold gold = new UserGold();
		gold.setUserId(userId);
		gold.setTotalAmount(0);
		gold.setActiveAmount(0);
		gold.setFrozenAmount(0);
		return gold;
	}

	/** 计算数据 */
	public UserGold calc(TradeTypeCore core, Integer amount) {
		if (amount != null) {
			char[] chars = core.operators;
			this.totalAmount = TradeTypeCore.calc(this.totalAmount, chars[0], amount);
			this.activeAmount = TradeTypeCore.calc(this.activeAmount, chars[1], amount);
			this.frozenAmount = TradeTypeCore.calc(this.frozenAmount, chars[2], amount);
		}
		return this;
	}

	public static final String USER_ID = "user_id";
	public static final String TOTAL_AMOUNT = "total_amount";
	public static final String ACTIVE_AMOUNT = "active_amount";
	public static final String FROZEN_AMOUNT = "frozen_amount";
	public static final String ADD_TIME = "add_time";
	public static final String UPDATE_TIME = "update_time";

}
